﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAOTicketReservationSystem;

namespace ManageEvents
{
    public partial class FormLocations : Form
    {
        private Dao dao;

        public FormLocations()
        {
            InitializeComponent();
        }

        private void FormEditLocations_Load(object sender, EventArgs e)
        {
            this.dao = new Dao();
            locationBindingSource.DataSource = this.dao.GetAllLocations();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.dao.Save();
            locationBindingSource.DataSource = dao.GetAllLocations();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            dao.Cancel();
            locationBindingSource.DataSource = dao.GetAllLocations();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewLocations.SelectedRows.Count > 0 && ((Location)dataGridViewLocations.SelectedRows[0].DataBoundItem) != null)
            {
                if (MessageBox.Show("You are going to delete selected location! Are you sure?", "Confirm delete",
                    MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Location currentLocation = (Location)dataGridViewLocations.SelectedRows[0].DataBoundItem;
                    string deleteLocationResult = dao.DeleteLocationById(currentLocation.LocationID);
                    if (deleteLocationResult == Dao.GetSuccessMessage())
                    {
                        MessageBox.Show("Location deleted!", "Success");
                        locationBindingSource.DataSource = dao.GetAllLocations();
                    }
                    else
                    {
                        MessageBox.Show(deleteLocationResult, "Error");
                    }
                    buttonCancel_Click(null, EventArgs.Empty);
                }
            }
            else
            {
                MessageBox.Show("Select location to delete!");
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
